package com.yuanluludev.app.activity

import android.Manifest
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import com.yuanluludev.app.R
import com.yuanluludev.app.adapter.TrackAdapter
import com.yuanluludev.app.bean.Person
import com.yuanluludev.lib_base.base.BaseActivity
import com.yuanluludev.lib_base.ext.toastTipMiddle
import com.yuanluludev.lib_base.utils.Bus
import com.yuanluludev.lib_utils.util.LogUtils
import com.yuanluludev.lib_webview.web.WebActivity
import com.yuanluludev.update.UpdateInfoBean
import com.yuanluludev.update.UpdateManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.collections.ArrayList

class MainActivity : BaseActivity() {

    private var per1: Person? = null
    private var per2: Person? = null
    private var mAdapter: TrackAdapter? = null

    val tag = "tag_"
    val tag1 = "tag_1"

    private val permissions = arrayOf(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_NETWORK_STATE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE,
    )

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initEvents() {

        settingBar?.getRightView()?.setOnClickListener {
           "点击了啊啊啊".toastTipMiddle()
        }

        //订阅
        Bus.observe<String>(tag, this) {
            LogUtils.e("=====1====")
        }

        //订阅
        Bus.observe<String>(tag, this) {
            LogUtils.e("=====2====")
        }

        val imagePaths = ArrayList<String>().apply {
            add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662712806&t=c578edef6f5de5b39d9d79bb772b9f57")
            add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F092919113248%2F1Z929113248-8-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662712806&t=2ca16a67aa867a2fc49d5a26d3e4fa7e")
            add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662712806&t=ef1b0700b5f83da155e397dcee27a62e")
            add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp06%2F20111116192A364-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662712806&t=850c2f06ce008ea18833b4da2468f3d3")
        }

        mAdapter = TrackAdapter(ArrayList()).apply {
            setOnItemChildClickListener { adapter, view, position ->
                val bean = adapter.data[position] as String
                when (view.id) {
                    R.id.ivImage -> {

//                        DragPhotoViewUtils.showDragPhotoView(
//                            view = view as ImageView,
//                            position = position,
//                            imagePaths = imagePaths
//                        )

                    }
                }
            }
        }

//        recyclerView?.apply {
//            layoutManager = GridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
//            adapter = mAdapter
//        }

//        mAdapter?.addData(imagePaths)

        //rlContainer
//        TimerPickerUtils.showTimePickerWithDecorView(
//            this,
//            rlContainer,
//            listener = {
//                LogUtils.e("" + TimeUtils.date2String(it))
//            },
//        )

        tvLoadFragment?.setOnClickListener {

        }

        setOnClickListener(
            R.id.tvLoadWebView,
            R.id.tvHelloWo,
            R.id.tvSave,
            R.id.tvGet,
            R.id.settingBar
        )

    }

    override fun onClick(v: View) {
        if (v.id == R.id.tvSave) {

//            Pgy.checkVersionUpdate(this, isShowTipDialog = true, isBackgroundDownload = false,)
//            MMKVUtils.getInstance().encode("SS", "年后")
//            Bus.post(tag, (""))
//            startActivity(Intent(this, NavigationActivity::class.java))

            UpdateManager.checkVersionUpdate(
                this,
                updateInfoBean = UpdateInfoBean(
                    // 服务器上的最新Version code
                    serverVersionCode = 220,
                    // 服务器上的最新Version code
                    serverVersionName = "2.2.0",
                    // 是否强制更新
                    isForceUpdate = true,
                    // 更新内容文案
                    updateContent = "1、测试一下啊  \n2、年后啊 \n3、年hi哦hi hi是和 \n4、jidisfhi \n5、nihishfsifhsdfhsdf",
                    // 新包下载地址
                    downloadUrl = "https://wap.pp.cn/app/dl/fs08/2022/11/16/7/106_35a8cb5513df5d8893172080a35c1522.apk",
                ),
                isBackgroundDownload = true,
                isShowTipDialog = true,
            )

        }
        if (v.id == R.id.tvGet) {
//            val ss = MMKVUtils.getInstance().decodeString("SS", "默认")
//            ss.toastTipMiddle()

        }
        if (v.id == R.id.tvLoadWebView) {
            LogUtils.e("-------")
//            CommonDialogUtils.showConfirmDialog(
//                this,
//                content = "确定要退出登录？"
//            )
//            CommonDialogUtils.showInputDialog(
//                this,
//                content = "确定要退出登录？",
//                inputContent = "你好",
//                hint = "亲输入"
//            )
//            FileReadActivity.jump(mContext,"/storage/emulated/0/Android/data/com.opro.roc_new/cache/opro/工作周报2022-袁路路（8月22日-8月26日) .xlsx")
//            WebActivity.jump(mContext,"http://183.230.62.60:20800/file-server/files/2022/8/25/b97a5cf9-08c3-4762-bc48-45c248e3e85a.mp4","视频详情")
//            WebActivity.jump(mContext,"http://183.230.62.60:20800/file-server/files/2022/9/1/6ae018de-ad65-45ae-825f-f76e6f5d516c.jpg")
            WebActivity.jump(getContext(), "https://mp.weixin.qq.com/s/GJJHjbg9QK93kD4YzHnrAQ")
//            FileReadActivity.jump(mContext,"/storage/emulated/0/Download/QQMail/万普隆评标评委组和评标方法暂行规定.pdf","哈哈哈")
//            WebActivity.jump(mContext,"http://183.230.62.60:20800/file-server/files/2022/9/1/c2708c77-c84e-4b6b-8769-2ad450aebaea.xlsx")
//            supportFragmentManager.beginTransaction()
//                .add(
//                    R.id.rlContainer,
//                    WebFragment.newInstance("https://www.baidu.com/", isShowTitleBar = false)
//                )
//             .commitNowAllowingStateLoss()
//            openPermission()

//            "点击申请权限".toastTipMiddle()

        } else if (v.id == R.id.tvHelloWo) {
            "点击了第二个".toastTipMiddle()
        } else if (v.id == R.id.settingBar) {
//           "点击了".toastTipMiddle()
        }

//            TimerPickerUtils.showTimePicker(
//                this,
//                timeType = TimerPickerUtils.TimeType.HOUR_MIN_SECOND,
//                endMills = 1735660800000,
//                currentMills = 1633448998000,
//                listener = { date, _ -> LogUtils.e("时间是 ====" + TimeUtils.date2Millis(date)) },
//            )
//        } else if (v.id == R.id.tvHelleell) {
//        }
    }

    override fun getLayoutResId() = R.layout.activity_main

}